package com.symc.his.api.impl.examination;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.symc.his.api.base.BaseApiService;
import com.symc.his.api.base.BaseResponse;
import com.symc.his.api.examination.ExaminationService;
import com.symc.his.api.examination.dto.req.CheckoutTermReqDTO;
import com.symc.his.api.examination.dto.req.InspectTermReqDTO;
import com.symc.his.api.examination.dto.resp.InspectTermRespDTO;
import com.symc.his.api.examination.dto.resp.InspectTermResultRespDTO;
import com.symc.his.api.examination.dto.resp.ItemFeeRespDTO;
import com.symc.his.api.impl.entity.CheckoutTermDO;
import com.symc.his.api.impl.entity.InspectTermDO;
import com.symc.his.api.impl.entity.ItemFeeDO;
import com.symc.his.api.impl.mapper.CheckoutTermMapper;
import com.symc.his.api.impl.mapper.InspectTermMapper;
import com.symc.his.api.impl.mapper.ItemFeeMapper;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @CreateTime: 2023/4/23 22:12
 * @Version:
 * @Since: 1.8
 * @Author: 辛凤文 forward.seen@foxmail.com
 */
@RestController
public class ExaminationServiceImpl extends BaseApiService implements ExaminationService {

    @Resource
    private InspectTermMapper inspectTermMapper;

    @Resource
    private CheckoutTermMapper checkoutTermMapper;

    @Resource
    private ItemFeeMapper itemFeeMapper;


    @Override public BaseResponse getCheckoutTerm(CheckoutTermReqDTO checkoutTermReqDTO) {
        Integer checkoutTermId = checkoutTermReqDTO.getCheckoutTermId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("checkout_term_id",checkoutTermId);
        CheckoutTermDO CheckoutTermRes = checkoutTermMapper.selectOne(queryWrapper);
        return setResultSuccess(CheckoutTermRes);
    }

    @Override public BaseResponse updateCheckoutTerm(CheckoutTermReqDTO checkoutTermReqDTO) {
        return null;
    }

    @Override public BaseResponse<InspectTermRespDTO> getInspectTermListById(Integer inspectTermId) {
        return null;
    }

    @Override
    public BaseResponse<List<String>> getInspectTermCodeListByVisitRecordIdAndTypeCode(String visitRecordId,
        String medicalOrderTypeCode) {
        LambdaQueryWrapper<InspectTermDO> queryWrapper = new LambdaQueryWrapper<InspectTermDO>()
            .eq(InspectTermDO::getVisitRecordId, visitRecordId)
            .eq(InspectTermDO::getMedicalOrderTypeCode, medicalOrderTypeCode);
        List<InspectTermDO> dos = inspectTermMapper.selectList(queryWrapper);
        List<String> inspectTermCodes = dos.stream().map(e -> e.getInspectTermCode()).collect(Collectors.toList());
        return setResultSuccess(inspectTermCodes);
    }

    @Override public BaseResponse<InspectTermRespDTO> getInspectTermListByVisitRecordId(String visitRecordId) {
        LambdaQueryWrapper<InspectTermDO> queryWrapper = new LambdaQueryWrapper<InspectTermDO>().eq(InspectTermDO::getVisitRecordId, visitRecordId);
        List<InspectTermDO> dos = inspectTermMapper.selectList(queryWrapper);
        List<InspectTermRespDTO> inspectList = dos.stream().map(e -> DO2DTO(e, InspectTermRespDTO.class)).collect(Collectors.toList());
        return setResultSuccess(inspectList);
    }

    @Override public BaseResponse insertInspectTerm(InspectTermReqDTO inspectTermReqDTO) {
        InspectTermDO inspectTermDO = DTO2DO(inspectTermReqDTO, InspectTermDO.class);
        int insertRes = inspectTermMapper.insert(inspectTermDO);
        if (insertRes < 1) {
            return setResultError();
        }
        return setResultSuccess();
    }

    @Override public BaseResponse updateInspectTerm(InspectTermReqDTO inspectTermReqDTO) {
        InspectTermDO inspectTermDO = DTO2DO(inspectTermReqDTO, InspectTermDO.class);
        String visitRecordId = inspectTermDO.getVisitRecordId();
        String inspectTermCode = inspectTermDO.getInspectTermCode();
        LambdaQueryWrapper<InspectTermDO> queryWrapper = new LambdaQueryWrapper<InspectTermDO>()
            .eq(InspectTermDO::getVisitRecordId, visitRecordId)
            .eq(InspectTermDO::getInspectTermCode, inspectTermCode);
        int updateRes = inspectTermMapper.update(inspectTermDO, queryWrapper);
        if (updateRes < 1) {
            return setResultError();
        }
        return setResultSuccess();
    }

    @Override public BaseResponse saveInspectTerm(InspectTermReqDTO inspectTermReqDTO) {
        return null;
    }

    @Override
    public BaseResponse deleteInspectTermByVisitRecordIdAndInspectTermCode(InspectTermReqDTO inspectTermReqDTO) {
        String inspectTermCode = inspectTermReqDTO.getInspectTermCode();
        String visitRecordId = inspectTermReqDTO.getVisitRecordId();
        LambdaQueryWrapper<InspectTermDO> queryWrapper = new LambdaQueryWrapper<InspectTermDO>()
            .eq(InspectTermDO::getInspectTermCode, inspectTermCode)
            .eq(InspectTermDO::getVisitRecordId, visitRecordId);
        int deleteRes = inspectTermMapper.delete(queryWrapper);
        if (deleteRes < 1) {
            return setResultError();
        }
        return setResultSuccess();
    }

    @Override public BaseResponse<ItemFeeRespDTO> getItemFeeInspectList() {
        List<ItemFeeDO> itemFeeDOS = itemFeeMapper.selectList(new LambdaQueryWrapper<ItemFeeDO>().eq(ItemFeeDO::getFeeCode,"55"));
        List<ItemFeeRespDTO> itemFeeResp = itemFeeDOS.stream().map(e -> DO2DTO(e, ItemFeeRespDTO.class)).collect(Collectors.toList());
        return setResultSuccess(itemFeeResp);
    }

    @Override public BaseResponse<String> getItemFeeNameByItemFeeCode(String itemFeeCode) {
        LambdaQueryWrapper<ItemFeeDO> queryWrapper = new LambdaQueryWrapper<ItemFeeDO>().eq(ItemFeeDO::getItemFeeCode, itemFeeCode);
        ItemFeeDO itemFee = itemFeeMapper.selectOne(queryWrapper);
        String feeName = itemFee.getItemFeeName();
        return setResultSuccess(feeName);
    }

    @Override
    public BaseResponse<InspectTermResultRespDTO> getInspectTermResultListByVisitRecordId(String visitRecordId) {
        LambdaQueryWrapper<InspectTermDO> queryWrapper = new LambdaQueryWrapper<InspectTermDO>().eq(InspectTermDO::getVisitRecordId, visitRecordId);
        List<InspectTermDO> inspectTermList = inspectTermMapper.selectList(queryWrapper);
        List<InspectTermResultRespDTO> resultResp = inspectTermList.stream().map(e -> DO2DTO(e, InspectTermResultRespDTO.class)).collect(Collectors.toList());
        return setResultSuccess(resultResp);
    }
}